import { Service } from 'egg';
const { Op } = require('sequelize');

export default class Rights extends Service {
  // 获取全部权限列表
  public async getAllRights() {
    const list = await this.ctx.model.Rights.findAll({
      attributes: {
        exclude: [ 'createdAt', 'updatedAt' ],
      },
    });
    return list;
  }
  // 获取权限列表(带查询条件)
  public async getRights(obj) {
    const currentPage = parseInt(obj.currentPage) || 1; // 当前页
    const pageSize = parseInt(obj.pageSize) || 5; // 页容量
    const { key, type } = obj;
    const { rightsType, level } = obj;
    // 关键字条件
    const defaultCondtion = {
      [Op.or]: [
        { rightsName: { [Op.substring]: key } },
        { rightsDesc: { [Op.substring]: key } },
      ],
    };
    if (key || type) { // 这里是顶部的类型和关键字的列表数据
      const conditionList: any[] = [];
      if (key) {
        conditionList.push(defaultCondtion);
      }
      if (type) {
        conditionList.push({ rightsType: type });
      }
      const rights = await this.ctx.model.Rights.findAll({
        attributes: {
          exclude: [ 'createdAt', 'updatedAt' ],
        },
        where: {
          [Op.and]: conditionList,
        },
        limit: pageSize, // 取多少条
        offset: (currentPage - 1) * pageSize, // 跳过多少条
      });
      // 总条数
      const count = await this.ctx.model.Rights.findAndCountAll({
        where: {
          [Op.and]: conditionList,
        },
      });
      return { rights, count: count.count };
    } else if (rightsType || level) { // 这里是判断新增权限中的下拉菜单列表
      const conditionList:any[] = [];
      if (rightsType) {
        conditionList.push({ rightsType });
      }
      if (level) {
        conditionList.push({ level });
      }
      const rights = await this.ctx.model.Rights.findAll({
        attributes: {
          exclude: [ 'createdAt', 'updatedAt' ],
        },
        where: {
          [Op.and]: conditionList,
        },
      });
      return { rights };
    }
    const rights = await this.ctx.model.Rights.findAll({
      attributes: {
        exclude: [ 'createdAt', 'updatedAt' ],
      },
      limit: pageSize, // 取多少条
      offset: (currentPage - 1) * pageSize, // 跳过多少条
    });
      // 总条数
    const count = await this.ctx.model.Rights.findAndCountAll();
    return { rights, count: count.count };

  }
  // 获取单个权限信息
  public async getRightsInfo(id) {
    return await this.ctx.model.Rights.findOne({
      attributes: {
        exclude: [ 'createdAt', 'updatedAt' ],
      },
      where: { id },
    });
  }
  // 新增权限
  public async addRights(obj) {
    const { rightsName } = obj;
    const name = await this.ctx.model.Rights.findOne({
      where: { rightsName },
    });
    if (name) {
      throw new Error('该权限也存在');
    }
    const data = await this.ctx.model.Rights.create(obj);
    const rightsData = data.dataValues;
    return rightsData;
  }
  // 删除权限
  public async destroy(id) {
    const rights = await this.ctx.model.Rights.findByPk(id);
    if (rights) {
      const data = await this.ctx.model.Rights.destroy({
        where: { id },
      });
      if (data > 0) {
        return data;
      }
    } else {
      throw new Error('该权限不存在');
    }
  }
  // 编辑权限
  public async updateRights(id, obj) {
    const name = await this.ctx.model.Rights.findByPk(id);
    if (name) {
      const data = await this.ctx.model.Rights.update(obj, {
        where: { id },
      });
      if (data.length > 0) {
        return data;
      }
      throw new Error('更新权限失败');
    } else {
      throw new Error('该权限不存在');
    }
  }
}
